//package com.plian.system.service.dg.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.dao.pm.property.PropertyRecordDao;
//import com.plian.system.dao.sys.file.FileDao;
//import com.plian.system.entity.dg.EnterpriseAssociation;
//import com.plian.system.mapper.dg.EnterpriseAssociationMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.dg.IChangeAssociationService;
//import com.plian.system.service.sys.file.FileService;
//import com.plian.system.vo.dg.EnterpriseAssociationVO;
//import com.plian.system.wrapper.dg.EnterpriseAssociationWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//import static com.plian.system.constant.sys.FormTypeConstant.DG_ENTERPRISE_ASSOCIATION_CHANGE;
//
///**
// * @Description: TODO
// * @Author: Take-off
// * @Date: 2023/4/4 5:05 PM
// * @Param
// * @return
// * @Version: 1.0
// **/
//@Service
//@AllArgsConstructor
//@Slf4j
//public class ChangeAssociationServiceImpl extends BaseServiceImpl<EnterpriseAssociationMapper,EnterpriseAssociation> implements IChangeAssociationService, BaseFlowService {
//    private final EnterpriseAssociationWrapper enterpriseAssociationWrapper;
//
//    private final FileService fileService;
//
//    private final FileDao fileDao;
//
//    private final PropertyRecordDao propertyRecordDao;
//
//    @Override
//    public boolean saveOrUpdate(EnterpriseAssociation enterpriseAssociation) {
//        boolean flag = false;
//        if (!Optional.ofNullable(enterpriseAssociation.getId()).isPresent()) {
//            enterpriseAssociation.setFormType(String.valueOf(FormTypeConstant.DG_ENTERPRISE_ASSOCIATION_CHANGE));
//            enterpriseAssociation.setVersion(enterpriseAssociation.getVersion() + 1);
//            enterpriseAssociation.setIsLatest(2);
//            enterpriseAssociation.setFileStatus(1);
//            enterpriseAssociation.setAssociationStatus(2);
//            enterpriseAssociation.setFailureTime(new Date());
//            updateIsLatest(enterpriseAssociation.getInitialId());
//        }
//        boolean result = super.saveOrUpdate(enterpriseAssociation);
//        if (flag){
//            fileService.saveAll(String.valueOf(enterpriseAssociation.getId()), enterpriseAssociation.getDocList());
//        }else {
//            fileService.edit(String.valueOf(enterpriseAssociation.getId()), enterpriseAssociation.getDocList());
//        }
//        return result;
//    }
//
//    @Override
//    @Transactional(rollbackFor=RuntimeException.class)
//    public boolean submit(EnterpriseAssociation enterpriseAssociation) {
//        boolean result = saveOrUpdate(enterpriseAssociation);
//        if (result){
//            saveNewProRecord(enterpriseAssociation);
//        }
//        return result;
//    }
//
//    private void updateIsLatest(Long initialId){
//        update(new LambdaUpdateWrapper<EnterpriseAssociation>().eq(EnterpriseAssociation::getInitialId, initialId)
//                .eq(EnterpriseAssociation::getIsLatest, 2).set(EnterpriseAssociation::getIsLatest, 1)
//                .set(EnterpriseAssociation::getFileStatus,2));
//    }
//
//    /**
//     * @Description: //TODO 保存一条新的产权记录
//     * @Author: Take-off
//     * @Date: 11:29 AM 2023/4/10
//     * @Param: [enterpriseAssociation]
//     * @return: void
//     **/
//    private void saveNewProRecord(EnterpriseAssociation enterpriseAssociation){
//        //上个版本
//        Long lastVersionId = enterpriseAssociation.getLastVersionId();
//        EnterpriseAssociation lastAssociation = getById(lastVersionId);
//        try {
//            List<PageData> oldFiles=  fileDao.findParentId(lastVersionId.toString());
//            if (CollectionUtil.isNotEmpty(oldFiles)){
//                PageData pageData = oldFiles.get(0);
//                String oldFileName = pageData.getString("fileName");
//                HashMap newFile = enterpriseAssociation.getDocList().get(0);
//                String fileName = newFile.get("fileName").toString();
//                if (!fileName.equals(oldFileName)){
//                    //拿到对应变动记录表数据
//                    PageData lastRecord = propertyRecordDao.findbyid(lastAssociation.getProId());
//                    PageData record = new PageData();
//                    String recordId = UuidUtil.get32UUID();
//                    record.putAll(lastRecord);
//                    record.put("id", recordId);
//                    record.put("updateTime", new Date());
//                    record.put("initialId", lastRecord.getString("initialId"));
//                    propertyRecordDao.save(record);
//
//                    //保存文件
//                    List<PageData> lastFiles = fileDao.findParentId(lastRecord.getString("id"));
//                    List<HashMap> recordFileList = new ArrayList<>();
//                    if (CollectionUtil.isNotEmpty(lastFiles)){
//                        for (PageData file : lastFiles){
//                            if ("f5".equals(file.getString("type"))){
//                                HashMap recordFile = new HashMap();
//                                recordFile.putAll(newFile);
//                                recordFile.put("id",  UuidUtil.get32UUID());
//                                recordFile.put("parentId", recordId);
//                                recordFileList.add(recordFile);
//                            }else {
//                                HashMap recordFile = new HashMap();
//                                recordFile.putAll(file);
//                                recordFile.put("id",  UuidUtil.get32UUID());
//                                recordFile.put("parentId", recordId);
//                                recordFileList.add(recordFile);
//                            }
//                        }
//                        fileDao.saveAll(recordFileList);
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    @Override
//    public HashMap<String, Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String, Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        List<EnterpriseAssociation> enterpriseAssociations = list(new LambdaQueryWrapper<EnterpriseAssociation>()
//                .in(EnterpriseAssociation::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA))));
//        List<EnterpriseAssociationVO> enterpriseAssociationVOS = enterpriseAssociationWrapper.entityToVO(enterpriseAssociations);
//        List<PageData> pageDataList = new ArrayList<>();
//        try {
//            if (CollectionUtil.isNotEmpty(enterpriseAssociationVOS)) {
//                for (EnterpriseAssociationVO vo : enterpriseAssociationVOS) {
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getOrgName());
//                    pd.put("APPLICANTNAME", vo.getCreateUser());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getOrgId());
//                    pd.put("formType", vo.getFormType());
//                    pd.put("orgName", vo.getOrgName());
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(DG_ENTERPRISE_ASSOCIATION_CHANGE);
//    }
//
//
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            EnterpriseAssociation enterpriseAssociation = getById(id);
//            if (updateStatus == FormStatusConstant.FINISH) {
//                enterpriseAssociation.setApprovedUser(TokenUtil.getTokenUserId());
//                Date now = DateUtil.now();
//                enterpriseAssociation.setApprovedTime(now);
//            }
//            enterpriseAssociation.setStatus(updateStatus);
//            updateById(enterpriseAssociation);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//}
